//客户管理

interface IClientManagentViewModel extends IBaseViewModel<IClientManagentQueryTerms> {
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<ClientListItemResponse>;
    /**
     * 商务专员列表
     */
    businessOfficerDropDown: EmployeeListItemResponse[];
    /**
     * 列表高
     */
    heightA:number;

     /**
     * 客户单位列表
     */
    clientList:ValueListItemResponse[];

    /**
     * 详情跳转
     */
    clientEdit: (item) => void;
   
}

interface IClientManagentQueryTerms {
    /**
     * 客户单位名称
     */
    name: string;
    /**
     * 商务专员编号
     */
    businessOfficerId: string;
    /**
     * 地址
     */
    address: string;
    /**
     * 省编号
     */
    provinceCode: string;
    /**
     * 市编号
     */
    cityCode: string;
    
   
    /**
     * 客户单位枚举
     */
    clientType:string
}

class ClientManagentController extends ListController<IClientManagentViewModel, IClientManagentQueryTerms> {
    gridApi: uiGrid.IGridApiOf<ClientListItemResponse>;

    skip:number = 0;

    constructor($scope: IClientManagentViewModel, private clientService: IClientService, private employeeService: IEmployeeService,private exportService:IexportService,
        private $ngBootbox: BootboxService,private valueService: IValueService,private $state: angular.ui.IStateService,private $location:ng.ILocationService) {
        super($scope);
        this.init();
    }

    init(): void {
        super.init();

        this.$scope.clientEdit = this.clientEdit;

        this.$scope.queryTerms = {
            name: "",
            businessOfficerId: "",
            address: "",
            cityCode: "",
            provinceCode: "",
            clientType:""
        };
        this.$scope.clientList = this.valueService.getClientTypeList().data;

        this.$scope.gridOptions = {
            enableSorting: false,
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            paginationPageSizes: [10, 20, 30],
            columnDefs: [
                { name: "序号", field: "index", enableColumnMenu: false, width: '5%' },
                { name: "客户单位", field: "name", enableColumnMenu: false },
                { name: "地址", field: "address", enableColumnMenu: false, width: '12%' },
                { name: "产品", field: 'products', enableColumnMenu: false },
                { name: "客户类型", field: 'clientTypeStr', enableColumnMenu: false },
                { name: "付款方式", field: 'paidWay', enableColumnMenu: false },
                { name: "商务专员", field: 'businessOfficer', enableColumnMenu: false, width: '10%' },
                { name: "录入时间", field: 'inputTime', enableColumnMenu: false },
                {
                    name: "操作",
                    cellFilter: 'checkmark',
                    // ui-sref="app.client.client_edit({id:row.entity.id})"
                    cellTemplate: '<a data-toggle="modal" title="编辑" data-target="#myModal" class="fa fa-edit m-l-xs primary text-info" ng-click="grid.appScope.clientEdit(row.entity)"></a>', enableColumnMenu: false, width: '5%'
                }
            ],
            onRegisterApi: (gridApi) => {
                // if(window.localStorage.getItem(String(this.$state.current.url+'Page'))){
                //     this.$scope.gridOptions.paginationCurrentPage=this.paginationOptions.currentPage;
                // };
                gridApi.pagination.on.paginationChanged(this.$scope,(skipSize,pageSize)=>{
                    this.paginationChanged(skipSize,pageSize,this.$state);
                });
            }
        };
        this.query();
        this.loadEmployeeDropDown();
    }

    loadData(queryTerms: IClientManagentQueryTerms, skip: number, count: number): void {
        this.clientService.getList(queryTerms.name, queryTerms.businessOfficerId,
            queryTerms.provinceCode, queryTerms.cityCode, queryTerms.address,queryTerms.clientType, skip, count).then(result => {
                this.$scope.gridOptions.totalItems = result.data.total;
                if (skip == 0) {
                    this.$scope.gridOptions.paginationCurrentPage = 1;
                }
                this.skip = skip;

                result.data.data.map((item, index) => item.index = skip++ +1);
                this.$scope.gridOptions.data = result.data.data;
                this.$scope.heightA = (this.$scope.gridOptions.data.length * 39 ) + 80;
        
                this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
            });
    }

    localHistory():void{
        super.localHistory(this.$state);
    }
    getHistory():void{
        super.getHistory(this.$state,this.$location)
    }

    clientEdit=(item)=>{
        this.$state.go("app.client.client_editNew", { name:'clientControllerEdit', id: item.id, skip:this.skip });
    }


    /**
     * 填充商务专员下拉列表
     */
    loadEmployeeDropDown(): void {
        this.employeeService.getbytype(CommissionerType.BusinessAffairs.toString(), 0, -1).then(result => {
            this.$scope.businessOfficerDropDown = result.data.data;
        });
    }

    /**客户单位导出 */
    exportFunc=()=>{
        if(this.$scope.gridOptions.totalItems>60000){
            this.$ngBootbox.alert('导出数据不可超过6万条');
            return;
        }else{
            this.exportService.exportClentlist(this.$scope.queryTerms.name, this.$scope.queryTerms.businessOfficerId, "","",this.$scope.queryTerms.address)
        }   
    }

}

angular.module("tms").controller("clientController", ['$scope', 'clientService', 'employeeService','exportService', '$ngBootbox','valueService','$state','$location', ClientManagentController]);